package com.javaweb.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/11/2 14:15
 */

@RestController
@RequestMapping("/web")
// context: http://localhost:8080/sat_chpt_04_springmvc/web/req
@Slf4j
public class WebObjectController {


//    @RequestMapping(value = "/req",method = RequestMethod.GET)
    @GetMapping("/req")
    public String processRequest(HttpServletRequest req) {
        // 可以获得 请求客户端诸多信息： ip, url, querystring ,user-agent
        log.info("url: {} \n, querystring: {}",req.getRequestURL(),req.getQueryString());
        log.info("user-agent: {}",req.getHeader("user-agent"));
        return "ok" ;
    }


    @GetMapping("/session")
    public String processSession(HttpSession session) {
        // 存放数据：key-value对存储。
        session.setAttribute("user","zhangsan") ;
        log.info("session with id:{}", session.getId()) ;
        return "ok + session id: " + session.getId() ;
    }

    @GetMapping("/session/further")
    public String processSessionFurther(HttpSession session) {
        // 获取数据
        log.info("get user from session:{}", session.getAttribute("user")) ;
        log.info("session with id:{}", session.getId()) ;
        return "ok + session id: " + session.getId() ;

    }

    @GetMapping("/cookie/add")
    public String processCookie(HttpServletResponse resp) {

        // 设置cookie
        Cookie cookie = new Cookie("user","zhangsan") ;
        cookie.setMaxAge(60 * 60 * 24 * 7) ;
        //将cookie存放在客户端
        resp.addCookie(cookie) ;
        return "ok" ;
    }
    @GetMapping("/cookie/get")
    public String processCookie(HttpServletRequest req) {
        // 获取cookie
        Cookie[] cookies = req.getCookies() ;
        for (Cookie cookie : cookies) {
            log.info("GOT cookie name: {}, value: {}",cookie.getName(),cookie.getValue()) ;
        }
        return "ok"  ;
    }

    @GetMapping("/cookie/submit")
    public String processCookie(@RequestParam String submitValue, HttpServletResponse resp) {
        // 设置cookie
        Cookie cookie = new Cookie("user",submitValue) ;
        cookie.setMaxAge(60 * 2) ;
        //将cookie存放在客户端
        resp.addCookie(cookie) ;
        log.info("a cookie was added.") ;
        return "ok" ;

    }

    @GetMapping("/cookie/return")
    public String returnCookie(HttpServletRequest req) {
        // 获取cookie
        Cookie[] cookies = req.getCookies() ;
        for (Cookie cookie : cookies) {
            if ("user".equals(cookie.getName())) {
                log.info("GOT cookie name: {}, value: {}", cookie.getName(), cookie.getValue());
                return cookie.getValue() ;
            }
        }
        return "None"  ;

    }





}
